package smashedapples.controls.progressbars
{
	import flash.display.DisplayObject;
	
	import mx.controls.ProgressBar;
	import mx.core.Container;
	import mx.core.IFlexDisplayObject;
	import mx.managers.PopUpManager;
	import mx.rpc.AsyncResponder;
	import mx.rpc.AsyncToken;
	
	public class AsyncTokenProgressBar extends ProgressBar
	{	
		
		public static function show(token:AsyncToken, parent:Object, label:String=null):void
		{	
			
			
			var container : AsyncTokenProgressBarContainer = new AsyncTokenProgressBarContainer();
				container.height = ( parent as DisplayObject).height;
				container.width = (parent as DisplayObject).width;
		
				
			var bar : AsyncTokenProgressBar = new AsyncTokenProgressBar();
				if(label!= null)
					bar.label = label;
				container.addChild( bar );
		
				
			PopUpManager.addPopUp( container, parent as DisplayObject, false );	
			PopUpManager.centerPopUp( container );
			
			bar.start(token);
			
		}
		
		
		
		public function AsyncTokenProgressBar()
		{
			super();
			this.indeterminate = true;
			this.mode = "manual";
			this.maximum = 0;
			this.minimum = 0;
			this.label = "";
		}

		
	
		public function start(token:AsyncToken):void
		{	
			this.setProgress( 0,1 );
			var responder : AsyncResponder = new AsyncResponder( handler, handler );
			token.addResponder( responder );	
				
		}
		
		public function stop():void
		{	
			this.setProgress( 1,1 );
			
			if( (this.parent as Container).isPopUp)
				PopUpManager.removePopUp(this.parent as IFlexDisplayObject);
			
		}
		
		private function handler(data:Object, token:Object=null):void
		{	
			stop();
		}
		
		
		
		
		

	}
}